package com.moresir.validatetools.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

public class ValidationData {

    /**
     * 获取星号前的数据
     */
    public static JSONArray extractDataFromBeforeAsterisksPath(String attribute, JSONObject masterData){
        if(StringUtils.isBlank(attribute)){
            return new JSONArray();
        }

        if(Objects.nonNull(masterData.get(attribute)) && masterData.get(attribute).getClass().equals(JSONArray.class)){
            return (JSONArray) masterData.get(attribute);
        }

        if(attribute.indexOf(".") > 0){
            String[] attributeArr = attribute.split("\\.");
            for (int i = 0; i < attributeArr.length; i++){
                String key = attributeArr[i];
                if(Objects.nonNull(masterData.get(key)) && masterData.get(key).getClass().equals(JSONObject.class) ){
                    masterData = (JSONObject) masterData.get(key);
                    continue;
                }
                if(Objects.nonNull(masterData.get(key)) && masterData.get(key).getClass().equals(JSONArray.class)){
                    if(i == attributeArr.length -1){
                        return (JSONArray) masterData.get(key);
                    }

                    int nextArrIndex = Integer.valueOf(attributeArr[i+1]);
                    if(
                            ((JSONArray) masterData.get(key)).size() > nextArrIndex
                    ){
                        masterData = (JSONObject) ((JSONArray) masterData.get(key)).get(nextArrIndex);
                        i++;
                        continue;
                    } else {
                        return null;
                    }
                }
            }
        }
        return null;
    }

    public static Object ExtractDataFromPath(String attribute, JSONObject masterData){
        if(StringUtils.isBlank(attribute)){
            return null;
        }

        if(masterData.containsKey(attribute)){
            return masterData.get(attribute);
        }

        if(attribute.indexOf(".") > 0){
            String[] attributeArr = attribute.split("\\.");
            for (int i=0; i < attributeArr.length; i++){
                String key = attributeArr[i];
                if(Objects.isNull(masterData)){
                    return null;
                }
                if(masterData.containsKey(key) && masterData.get(key).getClass().equals(JSONArray.class)){
                    if((((JSONArray) masterData.get(key)).size() <= Integer.valueOf(attributeArr[i + 1]))){
                        return null;
                    }

                    masterData = (JSONObject) ((JSONArray) masterData.get(key)).get(Integer.valueOf(attributeArr[i + 1]));
                    i++;
                    continue;
                }
                if(masterData.containsKey(key) && masterData.get(key).getClass().equals(JSONObject.class)){
                    masterData = (JSONObject) masterData.get(key);
                    continue;
                }
                if(masterData.containsKey(key)){
                    return masterData.get(key);
                }
            }
        }
        return null;
    }

}
